﻿using System.Collections.Generic;
using UnityEngine;
using System;
using System.Xml;
using System.Reflection;

namespace MGFramework {
    public class LocalConfigManager : MonoBehaviour
    {
        public static bool InitConfig() {
            try
            {
                return true;
            }
            catch {
                return false;
            }
        }

        /// <summary>
        /// 本地配置表
        /// </summary>
        public static void LoadLocalConfig()
        {
            
        }

        /// <summary>
        /// 加载XML数据
        /// </summary>
        /// <typeparam name="T">表名</typeparam>
        /// <returns></returns>
        public static List<T> Load<T>() where T : new()
        {
            string[] names = (typeof(T)).ToString().Split('.');
            string filename = names[names.Length - 1];
            XmlDocument doc = new XmlDocument();
            try
            {
                //打开xml配置文件
                TextAsset textAsset = (TextAsset)Resources.Load("Config/" + filename, typeof(TextAsset));
                doc.LoadXml(textAsset.text);
            }
            catch
            {
                Debug.LogError("--------------->获取" + filename + "配置文件错误<---------------");
            }
            XmlNode xmlNode = doc.DocumentElement;

            XmlNodeList xnl = xmlNode.ChildNodes;
            List<T> ret = new List<T>();
            foreach (XmlNode xn in xnl)
            {
                if (xn.Name == filename)
                {
                    T obj = new T();
                    Type t = obj.GetType();
                    FieldInfo[] fields = t.GetFields();
                    try
                    {
                        foreach (FieldInfo field in fields)
                        {
                            string val = xn.Attributes[field.Name].Value;
                            if (val == null)
                            {
                                Debug.Log("the field [" + field.Name + "] is null !!!");
                                continue;
                            }

                            if (field.FieldType == typeof(int))
                            {
                                field.SetValue(obj, int.Parse(val));
                            }
                            else if (field.FieldType == typeof(float))
                            {
                                field.SetValue(obj, float.Parse(val));
                            }
                            else if (field.FieldType == typeof(string))
                            {
                                field.SetValue(obj, val);
                            }
                            else if (field.FieldType == typeof(long))
                            {
                                field.SetValue(obj, long.Parse(val));
                            }

                        }
                        ret.Add(obj);
                    }
                    catch (Exception e)
                    {
                        Debug.LogError("--------------->读取" + filename + "文件错误<---------------");
                        Debug.LogError(e.Message);
                    }
                }
            }
            return ret;
        }
    }
}